<?php

namespace backend\models;

use common\helps\tools;
use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use yii\db\Exception;
use yii\db\Query;

/**
 * This is the model class for table "ProjectActor".
 *
 */
class ProjectActor extends ActiveRecord
{

    public $type;
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%project_actor}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return[
            [['name','position','role','avatar'], 'trim'],
            [['name','position','avatar'], 'required', 'message'=>'{attribute}不能为空'],
            [['id','type'], 'match', 'pattern' => '/^[1-9][0-9]*$/','message'=>'{attribute}值无效'],
        ];
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '演员id',
            'name' => '姓名',
            'position'=>'职业',
            'role'=>'角色',
            'project_id'=>'项目id',
            'avatar'=>'角色头像',
            'type'=>'操作类型',
        ];
    }


    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    public function scenarios()
    {
        return [
            'default' => [
                'name',
                'position',
                'role',
                'avatar',
                'created_at',
                'updated_at',
                'deleted_at',
                'type',
            ],
            'create' => [
                'name',
                'position',
                'role',
                'avatar',
                'project_id',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
            'update'=>[
                'id',
                'name',
                'position',
                'role',
                'avatar',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
            'delete'=>[
                'id',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
        ];
    }

    /**
     * 获取演职人员列表
     * @param $params
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function getProjectActorList($params){
        $list = $this->find()->select('*,FROM_UNIXTIME(created_at) as create_time')->asArray()->where(['deleted_at'=>null,'project_id'=>intval($params['project_id'])])->orderBy('sort ASC')->all();
        return empty($list)?array():$list;

    }

    /**
     * 获取演职人员信息
     * @param $params
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function getProjectActorInfo($params){
        if(!empty($params['name'])){
            $list = $this->find()->select('*,FROM_UNIXTIME(created_at) as create_time')->asArray()->where(['and',['deleted_at'=>null],['name'=>trim($params['name'])]])->all();
            return empty($list)?array():$list;
        }else{
            return [];
        }


    }


    /**
     * 删除演职人员
     * @param $params
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function delProjectActor($params){
        $model = static::findOne(['id' =>$params['id']]);
        if(empty($model)) return array('error'=>'未查询到数据信息');
        $model->scenario = 'delete';
        $data['deleted_at'] = time();
        $model->setAttributes($data, false);
        if (!$model->save()) {
            return array('error'=>'操作失败');
        }else{
            return array('error'=>'');
        }

    }



    /**
     * 新增与编辑演职人员
     * @param $param
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function editProjectActorInfo($param){
        if($param['type']>1){ //编辑
            $model = static::findOne(['id' =>$param['id']]);
            if(empty($model)) return array('error'=>'未查询到相关数据信息');
            $model->scenario = 'update';
        }else{
            $model = new self();
            $model->scenario = 'create';
            unset($param['id']);
        }
        $model->setAttributes($param, false);
        if (!$model->save()) {
            return array('error'=>'操作失败');
        }else{
            return array('error'=>'');
        }
    }


    /**
     * 删除与停启用项目
     * @param $param
     * @author hey_heyang(635870453@qq.com)
     * @return boolean
     */
    public  function editProjectState($param){
        $model = static::findOne(['id' =>$param['id']]);
        if(empty($model)) return array('error'=>'未查询到数据信息');
        $model->scenario = 'update';
        if($param['type']>1){ //删除
            $data['status']=3;
            $data['deleted_at'] = time();
        }else{ // 编辑停启用
            $info = $model->toArray();
            if($info['status']==1){
                $data['status'] = 2; //停用
            }elseif($info['status']==2){
                $data['status'] = 1; // 启用
            }else{
                return array('error'=>'参数错误,请重新刷新页面再次请求');
            }

        }
        $model->setAttributes($data, false);
        if (!$model->save()) {
            return array('error'=>'操作失败');
        }else{
            return array('error'=>'');
        }


    }


    /**
     * 新增与编辑项目类型
     * @param $param
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function editProjectInfo($param){
        if($param['type']>1){ //编辑
            $model = static::findOne(['id' =>$param['id']]);
            if(empty($model)) return array('error'=>'未查询到类型信息');
            if($param['type']==3){
                $model->scenario = 'update_project_analyse';
            }else{
                $model->scenario = 'update';
            }

        }else{
            $model = new self();
            $model->scenario = 'create';
            unset($param['id']);
        }
        if(!empty($param['tags'])){
            $tags = explode(',',$param['tags']);
            $param['tags']=serialize($tags);
        }
        if(!empty($param['release_time'])){
            $param['release_time'] = strtotime($param['release_time']);
        }
        $model->setAttributes($param, false);
        if (!$model->save()) {
            return array('error'=>'操作失败');
        }else{
            return array('error'=>'');
        }
    }



}
